package com.ruoyi.project.data.controller;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.service.PermissionService;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.domain.SysUser;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.data.domain.TbTransportWaybill;
import com.ruoyi.project.data.service.ITbTransportWaybillService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 运单Controller
 *
 * @author ruoyi
 * @date 2020-03-24
 */
@RestController
@RequestMapping("/weight/waybill")
public class TbTransportWaybillController extends BaseController
{
    @Autowired
    private ITbTransportWaybillService tbTransportWaybillService;
    @Autowired
    private PermissionService permissionService;

    /**
     * 查询运单列表
     */
    @PreAuthorize("@ss.hasPermi('weight:waybill:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbTransportWaybill tbTransportWaybill)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();

        // 场站id
        Long stationId = user.getStationId();
        tbTransportWaybill.setStationId(stationId);
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        tbTransportWaybill.setAreas(areas);
        tbTransportWaybill.setManagementCharacters(managementCharacters);
        startPage();
        List<TbTransportWaybill> list = tbTransportWaybillService.selectTbTransportWaybillList(tbTransportWaybill);
        return getDataTable(list);
    }

    /**
     * 导出运单列表
     */
    @PreAuthorize("@ss.hasPermi('weight:waybill:export')")
    @Log(title = "运单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbTransportWaybill tbTransportWaybill)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        tbTransportWaybill.setStationId(stationId);
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        tbTransportWaybill.setAreas(areas);
        tbTransportWaybill.setManagementCharacters(managementCharacters);
        List<TbTransportWaybill> list = tbTransportWaybillService.selectTbTransportWaybillList(tbTransportWaybill);
        ExcelUtil<TbTransportWaybill> util = new ExcelUtil<TbTransportWaybill>(TbTransportWaybill.class);
        return util.exportExcel(list, "运单列表");
    }

    @PreAuthorize("@ss.hasPermi('weight:waybill:export')")
    @Log(title = "运单", businessType = BusinessType.EXPORT)
    @GetMapping("/export2/{ids}")
    public AjaxResult export2(@PathVariable Long[] ids)
    {
        List<Map<String, Object>> list = tbTransportWaybillService.selectWaybillExport(ids);

        String tplPath =this.getClass().getResource("/").getPath() +"excel/运单.xls";
        String fileName = "江苏省公路货运运单.xls";

        //fileName = FileUtils.setFileDownloadHeader(request, fileName);

        Map<String, List<Map<String, Object>>> beanParams = new HashMap<String, List<Map<String, Object>>>();
        beanParams.put("list", list);

        XLSTransformer former = new XLSTransformer();
        InputStream in = null;
        OutputStream out = null;
        Workbook workbook = null;
        try {
            in = new BufferedInputStream(new FileInputStream(tplPath));
            workbook = former.transformXLS(in, beanParams);
            out = new FileOutputStream(ExcelUtil.getAbsoluteFile(fileName));
            workbook.write(out);
            return AjaxResult.success(fileName);
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (workbook != null)
            {
                try
                {
                    workbook.close();
                }
                catch (IOException e1)
                {
                    e1.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
        return AjaxResult.error("导出失败");
    }

    /**
     * 获取运单详细信息
     */
    @PreAuthorize("@ss.hasPermi('weight:waybill:query')")
    @GetMapping(value = "/{waybillId}")
    public AjaxResult getInfo(@PathVariable("waybillId") Long waybillId)
    {
        return AjaxResult.success(tbTransportWaybillService.selectTbTransportWaybillById(waybillId));
    }

    @PreAuthorize("@ss.hasPermi('weight:waybill:remove')")
    @Log(title = "运单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(tbTransportWaybillService.deleteWeightByWaybill(id));
    }

}
